package com.ymatou.query.seller.trading.api.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.Common;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductArguments;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductBean;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductResponse;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductResult;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductResult.IDText;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductResult.OrderCatelogAndPriceTypeInfos;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderLeaveWordByProductResult.OrderCommentInfo;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetSellerOrderLeaveWordByProduct;
import com.ymatou.query.seller.trading.api.parameter.Ymt_Order;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderInfo;
import com.ymatou.query.seller.trading.api.parameter.Ymt_Users;
import com.ymatou.query.seller.trading.api.service.GetSellerOrderLeaveWordByProductCall;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.common.entitie.C2COrderInfo.C2COrderInfoExt;
import com.ymttest.common.entitie.ShangouSellerOrdersByProduct;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取卖家指定商品订单留言 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetSellerOrderLeaveWordByProduct {

	private static GetSellerOrderLeaveWordByProductBean getsellerorderleavewordbyproductbean;
	private static GetSellerOrderLeaveWordByProductCall getsellerorderleavewordbyproductcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家指定商品订单留言");
	}

	@Before
	public void caseUp() {

		getsellerorderleavewordbyproductbean = new GetSellerOrderLeaveWordByProductBean();
		getsellerorderleavewordbyproductcall = new GetSellerOrderLeaveWordByProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerorderleavewordbyproductcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerOrderLeaveWordByProductResponse expectedResponse = new GetSellerOrderLeaveWordByProductResponse();
		ConstructExpectedResponse(expectedResponse);
		GetSellerOrderLeaveWordByProductResponse actualResponse = getsellerorderleavewordbyproductcall
				.getSellerOrderLeaveWordByProductResponse();
		DataUtil.verifyEquals(expectedResponse.getOrders().size(),
				actualResponse.getOrders().size(), "验证Orders的大小");
		if (expectedResponse.getOrders().size() == actualResponse.getOrders()
				.size()) {
			for (int i = 0; i < expectedResponse.getOrders().size(); i++) {
				ShangouSellerOrdersByProduct expect = expectedResponse
						.getOrders().get(i);
				ShangouSellerOrdersByProduct actual = actualResponse
						.getOrders().get(i);
				DataUtil.verifyEquals(expect.getOrderId(), actual.getOrderId(),
						"验证OrderId");
				DataUtil.verifyDateEquals(expect.getAddTime(),
						actual.getAddTime(), "验证AddTime");
				DataUtil.verifyEquals(expect.getBuyerId(), actual.getBuyerId(),
						"验证BuyerId");
				DataUtil.verifyEquals(expect.getBuyerName(),
						actual.getBuyerName(), "验证BuyerName");
				DataUtil.verifyEquals(expect.getCatalog(), actual.getCatalog(),
						"验证Catalog");
				DataUtil.verifyEquals(expect.getLeaveWord(),
						actual.getLeaveWord(), "验证LeaveWord");
				DataUtil.verifyEquals(expect.getPriceType(),
						actual.getPriceType(), "验证PriceType");
				DataUtil.verifyEquals(expect.getProductId(),
						actual.getProductId(), "验证ProductId");
			}
		}
	}

	private void ConstructExpectedResponse(
			GetSellerOrderLeaveWordByProductResponse response) {
		List<ShangouSellerOrdersByProduct> orders = new ArrayList<ShangouSellerOrdersByProduct>();
		GetSellerOrderLeaveWordByProductArguments args = new GetSellerOrderLeaveWordByProductArguments();
		args.setSellerId(getsellerorderleavewordbyproductbean.getSellerId());
		args.setLastOrderId(getsellerorderleavewordbyproductbean
				.getLastOrderId());
		args.setProductId(getsellerorderleavewordbyproductbean.getProductId());
		args.setTopCount(getsellerorderleavewordbyproductbean.getOrderNum());
		args.setOrderStatus(Common.OrderAvailiableStatusWithoutOrderEstablish());
		GetSellerOrderLeaveWordByProductResult result = execute(args);
		if (DataUtil.isNullOrEmpty(result.getOrderCommentsInfos())) {
			response.setOrders(orders);
			return;
		}
		for (OrderCommentInfo o : result.getOrderCommentsInfos()) {
			List<IDText> buyers = result.getBuyers().stream()
					.filter(x -> x.getId() == o.getiUserId())
					.collect(Collectors.toList());
			List<OrderCatelogAndPriceTypeInfos> propertyInfos = result
					.getOrderCatalogPropertyInfos().stream()
					.filter(x -> x.getId() == o.getiOrderId())
					.collect(Collectors.toList());
			ShangouSellerOrdersByProduct order = new ShangouSellerOrdersByProduct();
			order.setOrderId(o.getiOrderId());
			order.setProductId(getsellerorderleavewordbyproductbean
					.getProductId());
			order.setBuyerId(o.getiUserId());
			if (DataUtil.isNullOrEmpty(buyers)) {
				order.setBuyerName(DataUtil.Stringg.Empty);
			} else {
				order.setBuyerName(buyers.get(0).getText());
			}
			order.setLeaveWord(o.getsLeaveWord());
			order.setAddTime(o.getdAddTime());
			if (DataUtil.isNullOrEmpty(propertyInfos)) {
				order.setCatalog(DataUtil.Stringg.Empty);
				order.setPriceType(0);
			} else {
				order.setCatalog(propertyInfos.get(0).getText());
				order.setPriceType(propertyInfos.get(0).getiPriceType());
			}
			orders.add(order);
		}
		response.setOrders(orders);
	}

	private GetSellerOrderLeaveWordByProductResult execute(
			GetSellerOrderLeaveWordByProductArguments args) {
		Sp_GetSellerOrderLeaveWordByProduct sp = new Sp_GetSellerOrderLeaveWordByProduct();
		sp.setProductId(args.getProductId());
		sp.setSellerId(args.getSellerId());
		sp.setLastOrderId(args.getLastOrderId());
		sp.setOrderStatus(args.getOrderStatus());
		sp.setTop(args.getTopCount());
		sp.Execute();
		return ConvertResult(sp.getOrderList(), sp.getOrderInfoList(),
				sp.getUserList());
	}

	private GetSellerOrderLeaveWordByProductResult ConvertResult(
			List<Ymt_Order> orderList, List<Ymt_OrderInfo> orderInfoList,
			List<Ymt_Users> userList) {
		GetSellerOrderLeaveWordByProductResult result = new GetSellerOrderLeaveWordByProductResult();
		List<OrderCommentInfo> orderCommentsInfos = new ArrayList<GetSellerOrderLeaveWordByProductResult.OrderCommentInfo>();
		if (DataUtil.isNullOrEmpty(orderList) == false) {
			for (Ymt_Order o : orderList) {
				OrderCommentInfo oci = new OrderCommentInfo();
				oci.setdAddTime(o.getdAddTime());
				oci.setiOrderId(o.getiOrderId());
				oci.setiUserId(o.getiUserId());
				oci.setsAddress(o.getsAddress());
				oci.setsLeaveWord(o.getsLeaveWord());
				oci.setsPhone(o.getsPhone());
				oci.setsPostCode(o.getsPostCode());
				oci.setsReceivePerson(o.getsReceivePerson());
				oci.setsTelephone(o.getsTelephone());
				orderCommentsInfos.add(oci);
			}
		}
		List<OrderCatelogAndPriceTypeInfos> orderCatalogPropertyInfos = new ArrayList<GetSellerOrderLeaveWordByProductResult.OrderCatelogAndPriceTypeInfos>();
		if (DataUtil.isNullOrEmpty(orderInfoList) == false) {
			for (Ymt_OrderInfo i : orderInfoList) {
				OrderCatelogAndPriceTypeInfos ocapti = new OrderCatelogAndPriceTypeInfos();
				ocapti.setiCatalogStatus(i.getiCatalogStatus());
				ocapti.setId(i.getiOrderId());
				ocapti.setiPriceType(i.getiPriceType());
				ocapti.setText(i.getsPropertyInfo());
				orderCatalogPropertyInfos.add(ocapti);
			}
		}
		List<IDText> buyers = new ArrayList<GetSellerOrderLeaveWordByProductResult.IDText>();
		if (DataUtil.isNullOrEmpty(userList) == false) {
			for (Ymt_Users u : userList) {
				IDText it = new IDText();
				it.setId(u.getiUserId());
				it.setText(u.getsLoginId());
				buyers.add(it);
			}
		}
		result.setOrderCommentsInfos(orderCommentsInfos);
		result.setOrderCatalogPropertyInfos(orderCatalogPropertyInfos);
		result.setBuyers(buyers);
		return result;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderLeaveWordByProduct_001() {
		Logger.start(true, "获取卖家指定商品的订单留言成功");
		try {
			Logger.comment("准备数据");
			OrderCreate create = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt orderInfo = create.create4_ReceivedOrder(userId, sellerId);
			String ProductId = orderInfo.getsProductId();
			int orderId = orderInfo.getiOrderId();
			Logger.comment("数据准备结束");
			// 参数
			getsellerorderleavewordbyproductbean.setLastOrderId(orderId+1);
			getsellerorderleavewordbyproductbean.setOrderNum(5);
			getsellerorderleavewordbyproductbean.setProductId(ProductId);
			getsellerorderleavewordbyproductbean.setSellerId(sellerId);
			// 发送请求
			getsellerorderleavewordbyproductcall
					.setData(getsellerorderleavewordbyproductbean);
			getsellerorderleavewordbyproductcall.callService();
			Logger.comment("getsellerorderleavewordbyproductcall send:"
					+ getsellerorderleavewordbyproductcall.getEntityString());
			Logger.comment("getsellerorderleavewordbyproductcall response:"
					+ getsellerorderleavewordbyproductcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerorderleavewordbyproductcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderLeaveWordByProduct_002() {
		Logger.start(true, "商品id为空");
		try {
			Logger.comment("准备数据");
			OrderCreate create = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt orderInfo = create.create4_ReceivedOrder(userId,
					sellerId);
			int orderId = orderInfo.getiOrderId();
			Logger.comment("数据准备结束");

			// 参数
			getsellerorderleavewordbyproductbean.setLastOrderId(orderId + 1);
			getsellerorderleavewordbyproductbean.setOrderNum(5);
			getsellerorderleavewordbyproductbean.setProductId(null);
			getsellerorderleavewordbyproductbean.setSellerId(sellerId);
			// 发送请求
			getsellerorderleavewordbyproductcall
					.setData(getsellerorderleavewordbyproductbean);
			getsellerorderleavewordbyproductcall.callService();
			Logger.comment("getsellerorderleavewordbyproductcall send:"
					+ getsellerorderleavewordbyproductcall.getEntityString());
			Logger.comment("getsellerorderleavewordbyproductcall response:"
					+ getsellerorderleavewordbyproductcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400,
					getsellerorderleavewordbyproductcall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderLeaveWordByProduct_003() {
		Logger.start(true, "订单号id不存在");
		try {
			Logger.comment("准备数据");
			OrderCreate create = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt orderInfo = create.create4_ReceivedOrder(userId,
					sellerId);
			String ProductId = orderInfo.getsProductId();
			Logger.comment("数据准备结束");

			// 参数
			getsellerorderleavewordbyproductbean.setLastOrderId(1111);
			getsellerorderleavewordbyproductbean.setOrderNum(5);
			getsellerorderleavewordbyproductbean.setProductId(ProductId);
			getsellerorderleavewordbyproductbean.setSellerId(sellerId);
			// 发送请求
			getsellerorderleavewordbyproductcall
					.setData(getsellerorderleavewordbyproductbean);
			getsellerorderleavewordbyproductcall.callService();
			Logger.comment("getsellerorderleavewordbyproductcall send:"
					+ getsellerorderleavewordbyproductcall.getEntityString());
			Logger.comment("getsellerorderleavewordbyproductcall response:"
					+ getsellerorderleavewordbyproductcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerorderleavewordbyproductcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderLeaveWordByProduct_004() {
		Logger.start(true, "订单号id不存在");
		try {
			Logger.comment("准备数据");
			OrderCreate create = new OrderCreate();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			C2COrderInfoExt orderInfo = create.create4_ReceivedOrder(userId,
					sellerId);
			String ProductId = orderInfo.getsProductId();
			int orderId = orderInfo.getiOrderId();
			Logger.comment("数据准备结束");

			// 参数
			getsellerorderleavewordbyproductbean.setLastOrderId(orderId + 1);
			getsellerorderleavewordbyproductbean.setOrderNum(5);
			getsellerorderleavewordbyproductbean.setProductId(ProductId);
			getsellerorderleavewordbyproductbean.setSellerId(sellerId);
			// 发送请求
			getsellerorderleavewordbyproductcall
					.setData(getsellerorderleavewordbyproductbean);
			getsellerorderleavewordbyproductcall.callService();
			Logger.comment("getsellerorderleavewordbyproductcall send:"
					+ getsellerorderleavewordbyproductcall.getEntityString());
			Logger.comment("getsellerorderleavewordbyproductcall response:"
					+ getsellerorderleavewordbyproductcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerorderleavewordbyproductcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}